/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function createNotePad(){
    var div=document.createElement("div");
    div.setAttribute("id", "toast");
    div.style.position="absolute";
    div.style.zIndex="4";
    div.style.width="100%";
    div.style.height="60px";
    div.style.top=(getScroll().t+getBodyElement().clientHeight-50)+"px";
    div.innerHTML="<img src='img/bless/love.gif' title='送上祝福' style='cursor:pointer;' width='60px' height='40px' onclick='openNote(true)'/>"
        +"<span style='background-color:#E2EFFF'>祝福小提示： 1.新添祝福（单击爱心图标）  2.贴祝福（拖动便签纸） 3.保存祝福（单击小钉图标）</span>"
        +"<a href='javascript:void(0)' title='关闭提示' style='FONT-FAMILY: Webdings;' onclick='closeTip(this)'>r</a>";
    document.body.appendChild(div);
}
function closeTip(obj){
    obj.previousSibling.innerHTML="";
    obj.style.display="none";
}


function getRandomNotePad(){
    var t=(Math.ceil(Math.random()*5));
    return {
        "url":"img/index/Comment"+  t+".gif",
        "type":t
    };

}

function closeDiv(obj){
    var p= obj.parentNode.parentNode;
    p.style.display="none";
    p.parentNode.removeChild(p);
}

function openNote(moveable,name,content,lp,type){
    var noteHTML="";
    var urlType=getRandomNotePad();
    if(moveable){
        noteHTML+="<div  style='width:150px;height:10px;text-align:right;FONT-FAMILY: Webdings;font-size:18px'>"
        +"<a href='javascript:void(0)' title='保存' onclick='saveWish(this,"+urlType.type+")'>ë</a><a href='javascript:void(0)' title='关闭'  onclick='closeDiv(this)'>r</a>"
        +"</div>";
        noteHTML+="<div  style='cursor:move;width:160px;height:40px;top:20px;position:absolute;'></div>";
       noteHTML+="<div style='width:160px;height:150px;background-image:url("+urlType.url+")'> ";
        noteHTML +="<textarea title='写完后帖在墙的合适地方' id='content' style='width:115px;heigh:80px;left:20px;top:60px;position:absolute;overflow: auto;border:0;font-size:11px' rows='4'></textarea>";
        noteHTML +="<div style='width:135px;height:20px;text-align:right;top:130px;position:absolute;font-size:11px'>"
        +"<span>姓名:</span><input type='text' maxlength='4' style=';font-size:11px;border:0;height:12px' id='name' size='4'/></div>";
        noteHTML+= "</div>";
    }else{
        var shortContent=content;
        if(content.length>38){
            shortContent=content.substring(0,38)+"...";         
        }
        shortContent="&nbsp;&nbsp;&nbsp;&nbsp;"+shortContent;
        noteHTML+="<div style='width:160px;height:150px;background-image:url(img/index/Comment"+  type+".gif)'> ";
        noteHTML +="<div title='"+content+"' style='width:110px;heigh:80px;left:20px;top:50px;position:absolute;font-size:11px'>"+shortContent+"</div>";
        noteHTML +="<div style='width:140px;height:20px;text-align:right;top:120px;position:absolute;font-size:11px'><span>"+name+"</span></div>";
        noteHTML+= "</div>";
    }
    var noteDiv=document.createElement("div");
    noteDiv.style.position="absolute";
    noteDiv.style.zIndex="20";
    if(moveable){
        noteDiv.style.height="170px";
    }else{
        noteDiv.style.cursor="move";
        noteDiv.style.height="150px";
    }
    noteDiv.style.width="160px";
    noteDiv.innerHTML=noteHTML;
    if(moveable){
        var pos=mousePosition();
        var sc=getScroll();
        noteDiv.style.left=(sc.l+pos.x+10)+"px";
        noteDiv.style.top=(sc.t+pos.y-140)+"px";
        moveBind(noteDiv.childNodes[1]);
    }else{
        var lparr=lp.split(",");
        noteDiv.style.left=lparr[0];
        noteDiv.style.top=lparr[1];
        moveBind(noteDiv,true);
    }
    document.body.appendChild(noteDiv);
   
}
 
function moveBind(o,isThis){
	var x;
	var y;
    var pNode=o.parentNode;
    var movable=false;
    if(isThis)
        pNode=o;
  o.onselectstart = function(){
  return false;
  };
	if(!o.attachEvent && o.attachEventListener){// Firefox
		o.attachEventListener("onmousedown",function(event){handleMouseDown(pNode,event);});
		}else if(o.attachEvent){// IE
			o.attachEvent("onmousedown",function(event){handleMouseDown(pNode,event);});
		}
		else if(o.addEventListener){// safari 捕捉事件，第三个参数要设置为true
			o.addEventListener("mousedown",function(event){handleMouseDown(pNode,event);},true);
		}
    
// o.onselectstart = function(){
// return false;
// };
// o.onmousedown = function(e){
// if(pNode.setCapture)
// pNode.setCapture();
// else if(window.captureEvents)
// window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
//        
// e = e||window.event;
// if(curDiv!=pNode){
// if(curDiv)
// curDiv.style.zIndex='20';
// pNode.style.zIndex='99';
// }
// curDiv=pNode;
// x=e.layerX||e.offsetX;
// y=e.layerY||e.offsetY;
// 
// };
// o.onmousemove = function(e){
//     
// e = e||window.event;
// var sc=getScroll();
// pNode.style.left=(sc.l+e.clientX-x)+"px";
// pNode.style.top=(sc.t+e.clientY-y)+"px";
//    	 
// };
// o.onmouseup =function(){
// if(pNode.releaseCapture)
// pNode.releaseCapture();
// else if(window.captureEvents)
// window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
// }
    
}


function saveWish(obj,type){
    var pNode=obj.parentNode.parentNode;
    if(f$("content").value==""){
        alert("内容不能为空");
        return;
    }
    var ajax=new Ajax();
    ajax.requestURL = "add_bless.action";
    ajax.method = "GET";
    ajax.setVar("name", f$("name").value);
    ajax.setVar("content", f$("content").value);
    ajax.setVar("lp", pNode.style.left+","+pNode.style.top);
    ajax.setVar("type", type);
    ajax.onCompletion=function(){
        if(ajax.xmlhttp.responseText=="1"){
            alert("保存成功");
            window.location.reload();
        }
    }
    ajax.runAJAX();
}

function loadWish(){
    var ajax=new Ajax();
    ajax.requestURL = "load_bless.action";
    ajax.method = "GET";
    ajax.onCompletion=function(){
        var jsonObj= JSON.parse(ajax.xmlhttp.responseText);
        for(var i=0;i<jsonObj.length;i++){
            openNote(false,jsonObj[i].userName,jsonObj[i].commentContent,jsonObj[i].coordinate,jsonObj[i].type);
        }
    }
    ajax.runAJAX();
}




window.onload=function(){
    createNotePad();
    loadWish();

}
window.onscroll=function(){
	
    f$("toast").style.top=(getScroll().t+getBodyElement().clientHeight-50)+"px";
}
// scrollTop,scrollLeft,scrollWidth,scrollHeight
function getBodyElement()
{
        if (document.documentElement) {
                return  document.documentElement;
        } else  {
               return document.body;
        }   
}

function mousePosition(ev){
    ev = ev || window.event;
    if(ev.pageX || ev.pageY){
        return {
            x:ev.pageX-document.body.scrollLeft,
            y:ev.pageY-document.body.scrollTop
        };
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };
}


function getScroll(){
    	var t,l,w,h;
        t = document.documentElement.scrollTop ||document.body.scrollTop;
        l = document.documentElement.scrollLeft ||document.body.scrollLeft;
        w = document.documentElement.scrollWidth ||document.body.scrollWidth;
        h = document.documentElement.scrollHeight ||document.body.scrollHeight;
        
        return {
        t: t,
        l: l,
        w: w,
        h: h
    };

}